﻿// ______________________________________________________________
//
// Liam O'Donnell
// AssetLoader class manages loading assets using MovieClipLoader
// ______________________________________________________________
//

class com.spikything.utils.AssetLoader {

	// ______________________________________________________
	//
	// CONSTANTS
	// ______________________________________________________
	//

	var my_mcl		:MovieClipLoader;
	var loader 		:MovieClip;
	var loadbar 	:MovieClip;
	var myTarget 	:MovieClip;
	var host 		:MovieClip;
	var myListener	:Object;

	// ______________________________________________________
	//
	// CONSTRUCTOR
	// ______________________________________________________
	//

	function AssetLoader (host:MovieClip, loadbar:MovieClip) {
		
		// Create a MovieClipLoader object and its listener
		my_mcl 		= new MovieClipLoader();
		myListener 	= new Object();
		myListener.host = this;

		// Define event handlers for the loader's listener then add it
		myListener.onLoadStart = function(target_mc) {
			//loadbar.doSize(0);
			//loadbar.show();
		}
		myListener.onLoadComplete = function(target_mc) {
			//loadbar.hide();
		}
		myListener.onLoadProgress = function (target_mc, loadedBytes, totalBytes) {
			loadbar.doSize(100*(loadedBytes/totalBytes),target_mc);
		}
		myListener.onLoadInit = function (target_mc) {
			loadbar.hide();
			target_mc.isLoaded = true;
			target_mc._visible = false;
			host.assetLoaded(target_mc);
		}
		myListener.onLoadError = function(target_mc, errorCode) {
			host.assetLoaded(target_mc, errorCode);
		}
		my_mcl.addListener(myListener);

	}

	// ______________________________________________________
	//
	// METHODS
	// ______________________________________________________
	//

	// Load 'myAsset' into 'callTarget' using 'customLoadbar'
	public function loadAsset (myAsset:String, callTarget:MovieClip, customLoadbar:MovieClip, noLoader:Boolean):Void {
		if (callTarget.isLoaded && callTarget._url.indexOf(myAsset)>-1) return;
		
		if (!noLoader) {
			loadbar.doSize(0);
			loadbar.show();
		}
		
		myTarget = callTarget;
		myTarget.assetPath = myAsset;
		callTarget.isLoaded = false;
		my_mcl.loadClip(myAsset, myTarget);
	}
	
}